package net.url;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ContentHandler;
import java.net.URLConnection;
import java.util.Vector;

public class tab_separated_values extends ContentHandler
{
	@Override
	public Object getContent(URLConnection urlc) throws IOException
	{
		String theLine;
		Vector<String[]> lines = new Vector<String[]>();
		InputStreamReader isr = new InputStreamReader(urlc.getInputStream());
		BufferedReader in = new BufferedReader(isr);
		while((theLine=in.readLine())!=null)
		{
			String[] linearray = lineToArray(theLine);
			lines.addElement(linearray);
		}
		return lines;
	}
	private String[] lineToArray(String line)
	{
		int numFields = 1;
		for (int i = 0; i < line.length(); i++)
		{
			if(line.charAt(i)=='\t')
			{
				numFields++;
			}
		}
		String[] fields = new String[numFields];
		int position = 0;
		for (int i = 0; i < numFields; i++)
		{
			StringBuffer buffer = new StringBuffer();
			while(position<line.length()&&line.charAt(position)!='\t')
			{
				buffer.append(line.charAt(position));
				position++;
			}
			fields[i] = buffer.toString();
			position++;
		}
		return fields;
	}
	
}
